package com.yxj.checksystempicture.m;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.yxj.checksystempicture.u.CheckPictureUtil;
import com.yxj.checksystempicture.u.TakePhotoUtil;
import com.yxj.checksystempicture.i.TakePhotoResultListener;

public class CheckSystemPictureManager {


    private static final class ManagerHolder {
        static final CheckSystemPictureManager manager = new CheckSystemPictureManager();
    }

    public static CheckSystemPictureManager getInstance(){
        return ManagerHolder.manager;
    }


    private TakePhotoUtil takePhotoUtil;
    private CheckPictureUtil checkPictureUtil;
    private boolean isInit = false;
    /**
     * 初始化
     */
    public void init(Activity activity, String fileProvider, TakePhotoResultListener takePhotoResultListener){
        if (activity == null){
            throw new NullPointerException("activity is null") ;
        }

        if (TextUtils.isEmpty(fileProvider)){
             throw new NullPointerException("fileProvider is null") ;
        }

        if (takePhotoResultListener == null){
            throw new NullPointerException("takePhotoResultListener is null") ;
        }
        isInit = true ;
        checkPictureUtil = new CheckPictureUtil(activity).setTakePhotoResultListener(takePhotoResultListener);
        takePhotoUtil = new TakePhotoUtil(activity,fileProvider).setTakePhotoResultListener(takePhotoResultListener);
    }


    /**
     * 拍照
     */
    public void takePhoto(){
        if (!isInit){
            throw new RuntimeException("isInit is false,您还没初始化") ;
        }
        takePhotoUtil.takePhoto();
    }



    /**
     * 拍照
     */
    public void checkPicture(){
        if (!isInit){
            throw new RuntimeException("isInit is false,您还没初始化") ;
        }
        checkPictureUtil.open();
    }


    /**
     * 选择或者拍照结果
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (!isInit){
            throw new RuntimeException("isInit is false,您还没初始化") ;
        }
        takePhotoUtil.onActivityResult(requestCode, resultCode, data);
        checkPictureUtil.onActivityResult(requestCode,resultCode,data);
    }
}
